package com.example.sample.activity;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;

import com.example.app.BaseActivity;
import com.example.sample.R;

import net.liteheaven.xblib.widget.surfaceview.FloatSurfaceView;

public class FloatHeartActivity extends BaseActivity {

    private FloatSurfaceView mHeartSurface;
    private Timer mTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_float_heart);

        mHeartSurface = (FloatSurfaceView)findViewById(R.id.heartSurface);
        mHeartSurface.setHeartGenerator(new FloatSurfaceView.HeartGenerator());
        restartTimer();
    }

    private void startTimer(){
        if (mTimer == null){
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    mHeartSurface.addUnit(FloatSurfaceView.HeartGenerator.genColor());
                }
            };
            mTimer = new Timer();
            final int interval = 200;
            mTimer.schedule(timerTask, interval, interval);
        }
    }

    private void stopTimer(){
        if (mTimer != null){
            mTimer.cancel();
            mTimer = null;
        }
    }

    private void restartTimer(){
        stopTimer();
        startTimer();
    }
}
